﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MarvinsArena.Core;

namespace BattleEngineCommon
{
	public  class HudRadar
	{
		private Vector2 positionRadar;
		private Vector2 positionRadarBar;

		private Rectangle recOuter;
		private Rectangle recRadar;
		private Rectangle recRadarBar;
		private Rectangle recRobot;
		private Rectangle recWall;

		public Texture2D Texture { get; set; }

		public HudRadar(int ScreenWidth)
		{
            this.recRadar = new Rectangle(0, 141, 128, 128);
            this.recRadarBar = new Rectangle(0, 0, 128, 128);
            this.recRobot = new Rectangle(180, 15, 20, 20);
            this.recWall = new Rectangle(182, 79, 20,20);
            this.recOuter = new Rectangle(133, 142, 131, 131);

            this.positionRadar.X = ScreenWidth - recRadar.Width;
            this.positionRadar.Y = 0;

            this.positionRadarBar.X = this.positionRadar.X + recRadar.Width / 2;
            this.positionRadarBar.Y = this.positionRadar.Y + recRadar.Height / 2;
		}

		public void Draw(SpriteBatch spriteBatch, IRobot robot, List<IRobot> robots, IMap map)
		{

            //Drawing radar
			spriteBatch.Draw(this.Texture, positionRadar, this.recRadar, Color.White);
			spriteBatch.Draw(this.Texture, positionRadarBar, this.recRadarBar, Color.White, (float)robot.RotationRadar,
				new Vector2(this.recRadarBar.Width / 2.0f, this.recRadarBar.Height / 2.0f),
				1.0f, SpriteEffects.None, 0.5f);

            //Drawing robots in radar
            foreach (IRobot target in robots)
            {
                DrawObject(spriteBatch, robot, (IWorldObject)target, map);
            }

            //Drawing outer
			spriteBatch.Draw(this.Texture, positionRadar, this.recOuter, Color.White);

		}

        private void DrawObject(SpriteBatch spriteBatch, IRobot robot, IWorldObject target, IMap map)
        {
            Rectangle selectedRectangle = new Rectangle();

            //Set drawing texture and map accuracy
            switch (target.TypeWorldObject)
            {

                case TypeWorldObjects.Robot:
                    selectedRectangle = this.recRobot;
                    target.PositionX = (float)(Math.Ceiling(target.PositionX / map.MapScale) * map.MapScale);
                    target.PositionY = (float)(Math.Ceiling(target.PositionY / map.MapScale) * map.MapScale);
                    break;
                case TypeWorldObjects.Bullet:
                    break;
                case TypeWorldObjects.Missile:
                    break;
                case TypeWorldObjects.Wall:
                    selectedRectangle = this.recWall;
                    target.PositionX *= map.MapScale;
                    target.PositionY *= map.MapScale;
                    break;
                default:
                    break;
            }

            double angleBetweenObjects = Math.Atan2(target.PositionY - robot.PositionY, target.PositionX - robot.PositionX);
            double destinationToObject = Math.Sqrt(Math.Pow(target.PositionY - robot.PositionY, 2) + Math.Pow(target.PositionX - robot.PositionX, 2));

            //Zone radar vision
            float radarVision = 7 * map.MapScale;
            double CofScaleRadar = (this.recRadar.Height / 2.0f) / radarVision;
            float sizeObjectOnRadar = (float)(map.MapScale * CofScaleRadar);


            if (radarVision > Math.Floor(destinationToObject + (sizeObjectOnRadar / CofScaleRadar) / 2))
            {

                destinationToObject *= CofScaleRadar;

                Vector2 positionOnRadar = new Vector2();


                positionOnRadar.X = positionRadar.X + this.recRadar.Width / 2 - sizeObjectOnRadar + (float)(destinationToObject * Math.Cos(angleBetweenObjects));
                positionOnRadar.Y = positionRadar.Y + this.recRadar.Height / 2 - sizeObjectOnRadar + (float)(destinationToObject * Math.Sin(angleBetweenObjects));

                Rectangle ScaleObject = new Rectangle(Convert.ToInt32(positionOnRadar.X), Convert.ToInt32(positionOnRadar.Y),
                    Convert.ToInt32(sizeObjectOnRadar), Convert.ToInt32(sizeObjectOnRadar));

                spriteBatch.Draw(this.Texture, ScaleObject, selectedRectangle, Color.White);
            }
        }
	}
}
